package cn.exrick.manager.service.hotclassload;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class TestJDKProxy implements ExInterface {

    @Override
    public void execl() {
        System.out.println("jdk动态代理，执行中。。。。");
    }


    static class  JDKProxy implements InvocationHandler{

        Object target;
        public JDKProxy(Object target){

            this.target = target;
        }
        public <T> T createProxy(Class<T> cl){
           return  (T)Proxy.newProxyInstance(cl.getClassLoader(),cl.getInterfaces(),this);
        }
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("执行前置方法。。。");
            Object result = method.invoke(target,args);
            System.out.println("执行后置方法。。。。");
            return result;
        }
    }

    public static void main(String[] args) {
        JDKProxy jdkProxy = new JDKProxy(new TestJDKProxy());
        ExInterface exInterface = jdkProxy.createProxy(TestJDKProxy.class);
        exInterface.execl();

    }
}
